package poketdd.repository;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import poketdd.bean.Pokemon;

public class HibernatePokemonRepository implements PokemonRepository {

	private HibernateTemplate hibernateTemplate;

	public HibernatePokemonRepository(SessionFactory sessionFactory) {
		hibernateTemplate = new HibernateTemplate(sessionFactory);
	}

	public void registrarPokemon(Pokemon pokemon) {
		hibernateTemplate.save(pokemon);
	}

	@SuppressWarnings("unchecked")
	public Pokemon buscarPokemon(String codigo) {
		List<Pokemon> pokemons = hibernateTemplate.find(
				"from Pokemon p where p.codigo = ?", codigo);
		if (pokemons.isEmpty())
			return null;
		return pokemons.get(0);
	}

}
